<?php
declare (strict_types = 1);

namespace app\shop\controller;

use think\annotation\Route;
use think\annotation\route\Group;
use think\annotation\route\Middleware;
use app\common\service\StoreService\StoreManageService;
use app\BaseShopController;
use think\Request;
use app\Normal;

/**
 * @Group("Store")
 * @Middleware({shopMiddileware::class})
 */
class Store extends BaseShopController
{
    
    /**
     * 获取门店列表分页
     * @Route("getListByPage",method="GET")
     * @return string
     */
    public function getListByPage() {        
//        if(isset($this->_data['cb_id'])&& is_array($this->_data['cb_id'])&&$this->_master_user['cb_id']==0&&$this->_data['cb_id'][0]){
//            $this->_all_data['cb_id']=$this->_data['cb_id'];
//        }elseif($this->_master_user['cb_id']>0){
//            $this->_all_data['cb_id']=[$this->_master_user['cb_id']];
//        }else{
//            unset($this->_all_data['cb_id']);
//        }
//        $this->_all_data['cid']=[$this->_all_data['cid']];
//        if(isset($this->_all_data['mn_sid'])&&$this->_all_data['mn_sid'][0]==''){
//            unset($this->_all_data['mn_sid']);
//        }    
        $param= $this->_data;
        $param['cid']= $this->_master_user['cid'];
        if($this->_master_user['cb_id']>0){
            $param['cb_id']= $this->_master_user['cb_id'];
        }
        $result = callApi(StoreManageService::class, 'storeList',$param);
        return apiResponse($result);
    }
    
    /**
     * 启用禁用店铺
     * @Route("disableStore",method="PUT")
     * @return string
     */
    public function disableStore() {
        $result = callApi(StoreManageService::class, 'changeStorestatus',$this->_all_data);
        if($result['code']==\app\Normal::SUC){
            $this->log(['content'=>$this->_all_data['status']?'启用':'禁用'.'店铺id为【'.$this->_data['id'].'】', 'request_param'=>json_encode($this->_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
    /**
     * 获取门店下拉列表
     * @Route("storeSelectList",method="GET")
     * @return string
     */
    public function storeSelectList() {
        $param= $this->_data;
        $param['cid']= $this->_master_user['cid'];
        if(isset($param['cb_id'])&&is_array($param['cb_id'])&&$param['cb_id'][0]>0){
            $param['cb_id']=$this->_data['cb_id'];
        }elseif($this->_master_user['cb_id']>0){
            $param['cb_id']= [$this->_master_user['cb_id']];
        }
        $result = callApi(StoreManageService::class, 'storeSelectList',$param);
        return apiResponse($result);
    }
    
    /**
     * 刷新店铺二维码
     * @Route("refreshQrcode/:mn_sid",method="GET")
     * @return string
     */
    public function refreshQrcode($mn_sid) {
        $result = callApi(StoreManageService::class, 'makeStoreQrcode', $this->_all_data);
        if($result['code'] == Normal::SUC){
            $this->log(['content'=>'刷新店铺二维码','request_param'=> json_encode($this->_all_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
    
    /**
     * 获取门店审核列表分页
     * @Route("getStoreCheckListByPage",method="GET")
     * @return string
     */
    public function getStoreCheckListByPage() {        
        if(isset($this->_data['cb_id'])&& is_array($this->_data['cb_id'])&&$this->_master_user['cb_id']==0&&$this->_data['cb_id'][0]){
            $this->_all_data['cb_id']=$this->_data['cb_id'];
        }elseif($this->_master_user['cb_id']>0){
            $this->_all_data['cb_id']=[$this->_master_user['cb_id']];
        }else{
            unset($this->_all_data['cb_id']);
        }
        $this->_all_data['cid']=[$this->_all_data['cid']];
        if(isset($this->_all_data['mn_sid'])&&$this->_all_data['mn_sid'][0]==''){
            unset($this->_all_data['mn_sid']);
        }        
        $result = callApi(StoreManageService::class, 'getStoreCheckListByPage',$this->_all_data);
        return apiResponse($result);
    }
    
    /**
     * 审核店铺
     * @Route("checkStore",method="PUT")
     * @return string
     */
    public function checkStore() {
        $result = callApi(StoreManageService::class, 'checkStore',$this->_all_data);
        if($result['code']==\app\Normal::SUC){
            $this->log(['content'=>$this->_all_data['op']==1?'审核通过':'审核失败'.'店铺id为【'.$this->_data['id'].'】', 'request_param'=>json_encode($this->_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
}
